function welfare = RamseyAffineObjective(tau1,pr,w,pai,pw,G)
% Input  : tau1  = linear tax rate
%          pr    = parameters
%          w     = exp(alpha)
%          pai   = probablity messure
%          G     = government expenditure
% Output : welfare= value of the objective function

load c
flag = 0;

tau0 = G-tau1.*(1-tau1).^(1/pr.sig).*pr.Om.^(-1/pr.sig).*sum(pai.*c.^-(pr.gam/pr.sig).*w.^((1+pr.sig)/pr.sig));
            
for iter = 1:50
%c = zeroMAT(c./20,c.*20,1e-10,@(x) RamseyAffineObjectiveFOC(x,tau0,tau1,pr,w));
c = zeroMAT(c./20000000,c.*20000000,1e-10,@(x) RamseyAffineObjectiveFOC(x,tau0,tau1,pr,w));
    if max(RamseyAffineObjectiveFOC(c,tau0,tau1,pr,w))>1e-5
        flag=1;
        break
    end
    t0 = G-tau1.*(1-tau1).^(1/pr.sig).*pr.Om.^(-1/pr.sig).*sum(pai.*c.^-(pr.gam/pr.sig).*w.^((1+pr.sig)/pr.sig));

    if max(abs(tau0-t0))<1e-8
        break
    else
        tau0 = t0;
    end
end

if max(flag) ==1
    welfare = NaN;
    disp('flag = 1')
else
    y = c.^-(pr.gam/pr.sig).*(1-tau1).^(1/pr.sig).*pr.Om.^(-1/pr.sig).*w.^((1+pr.sig)/pr.sig);
    welfare = sum(pw.*pai.*Utility(c,y,w,pr));
end

tau = [tau0 tau1];
disp([welfare tau])
save tau tau
save c c
save y y 
